<?php
  session_start();
  error_reporting(0);
  $error = $_GET['error'];
  include 'test.php';
  //User tried to access without loggin in.
  $username = $_SESSION['username'];
  if($username == "")
  {
    header("Location:index.php?error=Please login first.");
  }
  
  if(isset($_POST['submit'])) {
    $stock = strtoupper($_POST['stock']);
    
    $myFile = "./stockPort.csv";
    $owned = alreadyOwn($stock);
    if($owned[0]) {
        $arStocks = getStocks();
        $newStocks = "";
        $pattern = "/^" . $stock . "/";
        foreach ($arStocks as $entry) {
            
            if (!preg_match($pattern, $entry)) {
                $newStocks = $newStocks . $entry;
            }
        }
        file_put_contents($myFile, $newStocks);
		header("Location:admin.php?status=Success!");
    }
    else {        
        header("Location:admin.php?error=You don't own $stock!");
    }
  }?>

<html>
<head>
<title>Delete a stock</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" type="text/css" href="../css/bootstrap.css">
<style>
	input[type="text"] {
	height: 30px;
	}
	a {
	text-decoration: none;
	}
</style>
</head>
<body style="background-color: #CCCCCC">
<div class="container">
<div class="hero-unit">
        <h1>Delete a position, <?php echo strtoupper($username);?></h1>
	</div>
<form class="form-horizontal" method="post">
  <div class="control-group">
    <label class="control-label" for="stock">Stock Symbol</label>
    <div class="controls">
      <input type="text" name="stock" id="stock" placeholder="Ticker Symbol">
    </div>
  </div>
  <div class="control-group">
    <div class="controls">
      <button type="submit" name="submit" class="btn">Delete</button>
	  <a class="btn" href="admin.php">Cancel</a>
    </div>
  </div>
</form>
<div class="row">
	<div class="span12">
		<h2>Current Positions</h2>
		<?php include("viewFrame.php"); ?>
	</div>
</div>
</body>
</html>
